﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Sys_Config_Add(Sys_Config model ,int ACID);


[OperationContract]
string Sys_Config_Update(Sys_Config model ,int ACID);


[OperationContract]
string Sys_ConfigByid_Delete(int id,int ACID);


[OperationContract]
Sys_Config Sys_ConfigByid_Get(int id,int ACID );


[OperationContract]
List<Sys_Config> Sys_Config_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Sys_Config表数据
/// <summary>
/// 添加Sys_Config
/// </summary>
/// <param name="model">Sys_Config实体</param>
/// <returns>空或错误</returns>
public string Sys_Config_Add(Sys_Config model,int ACID)
{
if(model.id==null )
{
return "『设置项ID』不能为空！";
}
if(string.IsNullOrEmpty(model.itemvalue))
{
return "『设置项值』不能为空！";
}
if(string.IsNullOrEmpty(model.item))
{
return "『设置项名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sys_Config.Count(p=>p.id==model.id)>0)
{
return "主键已存在！";
}
ctx.Sys_Config.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Sys_Config表数据事物用重构
/// <summary>
/// 添加Sys_Config事物用重构
/// </summary>
/// <param name="model">Sys_Config实体</param>
/// <returns>空或错误</returns>
private string Sys_Config_Add(Sys_Config model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.id==null )
{
return "『设置项ID』不能为空！";
}
if(string.IsNullOrEmpty(model.itemvalue))
{
return "『设置项值』不能为空！";
}
if(string.IsNullOrEmpty(model.item))
{
return "『设置项名称』不能为空！";
}
if(ctx.Sys_Config.Count(p=>p.id==model.id)>0)
{
return "主键已存在！";
}
ctx.Sys_Config.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Sys_Config表数据
/// <summary>
/// 修改Sys_Config
/// </summary>
/// <param name="model">Sys_Config实体</param>
/// <returns>空或错误</returns>
public string Sys_Config_Update(Sys_Config model,int ACID)
{
if(model.id==null )
{
return "『设置项ID』不能为空！";
}
if(string.IsNullOrEmpty(model.itemvalue))
{
return "『设置项值』不能为空！";
}
if(string.IsNullOrEmpty(model.item))
{
return "『设置项名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sys_Config.Count(p=>p.id==model.id)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Sys_Config Sys_Configmodel=ctx.Sys_Config.Single(p => p.id==model.id);
if(model.id!=null )
{
Sys_Configmodel.id=model.id;
}
if(!string.IsNullOrEmpty(model.item))
{
Sys_Configmodel.item=model.item;
}
if(!string.IsNullOrEmpty(model.itemvalue))
{
Sys_Configmodel.itemvalue=model.itemvalue;
}
if(!string.IsNullOrEmpty(model.remarks))
{
Sys_Configmodel.remarks=model.remarks;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Sys_Config表Byid数据
/// <summary>
/// 删除Sys_ConfigByid
/// </summary>
/// <param name="id">设置项ID</param>
/// <returns>空或错误</returns>
public string Sys_ConfigByid_Delete(int id,int ACID)
{
if(id==null )
{
return "『设置项ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sys_Config.Count(p=>p.id==id)!=1)
{
return null;
}
Sys_Config model=ctx.Sys_Config.Single(p => p.id== id);
ctx.Sys_Config.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Sys_Config表Byid数据[事物用重构]
/// <summary>
/// 删除Sys_ConfigByid[事物用重构]
/// </summary>
/// <param name="id">设置项ID</param>
/// <returns>空或错误</returns>
private string Sys_ConfigByid_Delete(int id,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(id==null )
{
return "『设置项ID』不能为空！";
}
if(ctx.Sys_Config.Count(p=>p.id==id)!=1)
{
return null;
}
Sys_Config model=ctx.Sys_Config.Single(p => p.id== id);
ctx.Sys_Config.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Sys_Config表Byid数据
/// <summary>
/// 获取Sys_ConfigByid
/// </summary>
/// <param name="id">设置项ID</param>
/// <returns>当前表</returns>
public Sys_Config Sys_ConfigByid_Get(int id,int ACID)
{
if(id==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Sys_Config.Count(p=>p.id==id)!=1)
{
return null;
}
return ctx.Sys_Config.SingleOrDefault(p=>p.id==id);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Sys_Config表数据
/// <summary>
/// 获取Sys_Config
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Sys_Config></returns>
public List<Sys_Config> Sys_Config_Get(Expression<Func<Sys_Config, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sys_Config.ToList();
}
 else 
{
return ctx.Sys_Config.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sys_Config.Where(predicate).ToList();
}
 else 
{
return ctx.Sys_Config.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Sys_Config表数据
/// <summary>
/// 获取Sys_Config
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Sys_Config></returns>
public List<Sys_Config> Sys_Config_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Sys_Config_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Sys_Config表数据计数
/// <summary>
/// 获取Sys_Config表数据计数
/// </summary>
/// <returns>List<Sys_Config></returns>
public int Sys_Config_GetCount(Expression<Func<Sys_Config, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Sys_Config.Count();

}
 else
{
return ctx.Sys_Config.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Sys_Config分页表数据
/// <summary>
/// 获取Sys_Config分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Sys_Config></returns>
public List<Sys_Config> Sys_Config_Getpage(int pageIndex, int pageSize,Expression<Func<Sys_Config, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sys_Config.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Sys_Config.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Sys_Config.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Sys_Config.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Sys_Config表数据
/// <summary>
/// 批量修改Sys_Config
/// </summary>
/// <param name="predicate">Expression<Func<Sys_Config, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Sys_Config,Sys_Config>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Sys_Config_batchUpdate(Expression<Func<Sys_Config, bool>> predicate,Expression<Func<Sys_Config, Sys_Config>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Sys_Config.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Sys_Config表数据事物用重构
/// <summary>
/// 批量修改Sys_Config事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Sys_Config, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Sys_Config,Sys_Config>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Sys_Config_batchUpdate(Expression<Func<Sys_Config, bool>> predicate,Expression<Func<Sys_Config, Sys_Config>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Sys_Config.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Sys_Config表数据
/// <summary>
/// 批量删除Sys_Config
/// </summary>
/// <param name="predicate">Expression<Func<Sys_Config, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Sys_Config_batchDel(Expression<Func<Sys_Config, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Sys_Config.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Sys_Config表数据事物用重构
/// <summary>
/// 批量删除Sys_Config事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Sys_Config, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Sys_Config_batchDel(Expression<Func<Sys_Config, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Sys_Config.Delete(predicate);
return ok;
}
#endregion


}
}

